package com.intel.factorytest.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.intel.factorytest.R;
import com.intel.factorytest.cfparser.ConfigFileParseUtil;
import com.intel.factorytest.cfparser.concrete.AConfigFileParser;
import com.intel.factorytest.cfparser.concrete.PropertiesConfParser;
import com.intel.factorytest.constants.PropertiesConstants;
import com.intel.factorytest.util.PropertiesUtil;

public class SoftwareInfo extends BaseActivity{
    
    private ListView mListView;
    private String mSWBuildVersion;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.listview_with_button);
        initView();
        initSoftwareInfosProps();
        
        mListView = (ListView) findViewById(R.id.ListView01);
        
        //生成动态数组，加入数据  
        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();  
        
        HashMap<String, String> map = new HashMap<String, String>();  
        map.put("ItemTitle", getString(R.string.SWVERSION)); 
        map.put("ItemText", getBuildNum());  
        listItem.add(map);  
        
        //生成适配器的Item和动态数组对应的元素  
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItem,//数据源   
            R.layout.item_listview_with_button_softwareinfo,//ListItem的XML实现  
            //动态数组与对应的子项          
            new String[] {"ItemTitle", "ItemText"},   
            //ImageItem的XML文件里面的一个ImageView,两个TextView ID  
            new int[] {R.id.ItemTitle,R.id.ItemText}  
        );  
         
        //添加并且显示  
        mListView.setAdapter(listItemAdapter);  
        
        //检查版本是否匹配
        mHandler.sendEmptyMessageDelayed(0, 2000);
    }
    
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            checkResult();
        };
    };
    
    private String getBuildNum() {
        return Build.DISPLAY;
//        return Build.SOFTWARE;
    }
    
    private void initSoftwareInfosProps() {
        PropertiesConfParser propParser = (PropertiesConfParser) ConfigFileParseUtil.getConfParserConcrete(this, AConfigFileParser.EXTENSION_PROPERTISE);
        PropertiesUtil properties = propParser.getProperties(isPcba);
        mSWBuildVersion = properties.getProperty(PropertiesConstants.KEY_SWINFO_BUILD_VERSION, "");
    }
    
    private void checkResult() {
        System.out.println("tangjun check swinfo : BUILD.VERSION=" + getBuildNum() + ", ConfigV=" + mSWBuildVersion);
        if (isMatch(getBuildNum(), mSWBuildVersion)) {
            is_pass = true;
            finish();
        } else if (mSWBuildVersion != null && !mSWBuildVersion.equals("")) {
            mSucBtn.setVisibility(View.INVISIBLE);
        }
    }
    
    private boolean isMatch(String source, String target) {
        if (source == null || target == null) {
            return false;
        }
        return source.equals(target);
    }
    
    @Override
    public void finish() {
        // TODO Auto-generated method stub
        setTestResult();
        super.finish();
    }
}
